'use strict';

import Base from './base.js';
import path from 'path';
import fs from 'fs';
import { transliterate as tr, slugify } from 'transliteration';
export default class extends Base {
  /**
   * index action
   * @return {Promise} []
   */
  async indexAction() {
    const model = this.model('upload')
    if (this.isPost()) {
      var file = think.extend({}, this.file('file'));
      var filepath = file.path;
      //文件上传后，需要将文件移动到项目其他地方，否则会在请求结束时删除掉该文件
      var uploadPath = think.RESOURCE_PATH + '/upload';
      think.mkdir(uploadPath);
      var basename = path.basename(filepath);
      fs.renameSync(filepath, uploadPath + '/' + basename);
      file.path = uploadPath + '/' + basename;
      const url = 'http://oc.youbanghelper.com/images/' + basename;
      const localUrl = file.path;
      const ctime = think.datetime();
      const user = JSON.parse(this.post('user'))
      const weixinname = user.nickName;
      const city = user.city;
      const tage = slugify(this.post('tage'),{separator:''});
      const res = await model.add({url,localUrl,ctime,weixinname,city,tage})
      return this.success(res)
    } else {
      this.fail('http methods no support')
    }
  }
  saveImage(file) {

  }
}